--#include "data\config\activity\ExpRefiningConfig.lua" once
--#include "data\config\property\LevelUpExp.lua" once

--经验炼制
MiscsImplement.GetExpRefining = function(sysarg, msgid, packet)
	local nOptType = DataPack.readByte(packet);
	if nOptType == 1 then
		SendExpRefiningTimes(sysarg);
	else
		local nOpenDay = System.getDaysSinceOpenServer()
		if nOpenDay > #ExpRefiningConfig.ExpRefining then
			return
		elseif not Actor.checkActorLevel(sysarg,ExpRefiningConfig.neelLevel) then
			Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.COM0055, ExpRefiningConfig.neelLevel), ttFlyTip)
			return
		end
		
		local config = ExpRefiningConfig.ExpRefining[nOpenDay]
		if not config then
			return
		end
		
		local var = Actor.getStaticVar(sysarg)
		if not var.days then var.days = {} end
		local ExpRefiningCount = var.days.ExpRefiningCount
		if ExpRefiningCount == nil then ExpRefiningCount = 0 end
		
		if ExpRefiningCount >= ExpRefiningConfig.maxCountDay then
			return
		end
			
		local itemCfg = config[ExpRefiningCount + 1]
		if itemCfg == nil then return end	

		local ReachLevel,nLastExp = Actor.AddExpReachLevel(sysarg,itemCfg.addExp, 0)  

		local circle = Actor.getUIntProperty(sysarg, PROP_ACTOR_CIRCLE)
		local maxLevel = System.getPlayerConfigMaxLevelByCircle(circle)
		if  itemCfg.addExp > 0 and nLastExp >= itemCfg.addExp then
		    local str = Lang.ScriptTips.ExpItem001
		    Actor.sendTipmsg(sysarg,str,ttFlyTip)
			return
		end

		local result = CheckConsumeCond( sysarg, {itemCfg.consume}, 1 )

		if not result or result == false then 
			return 
		end

		result = DoConsumeCond( sysarg, {itemCfg.consume}, 1, GameLog.cNewGiveExpRefining, Lang.LogTips.ExpRefiningLog )
		if not result or result == false then
			return
		end
		
		Actor.addExp(sysarg,itemCfg.addExp, GameLog.cNewGiveExpRefining, 0)
		var.days.ExpRefiningCount = ExpRefiningCount + 1
		System.broadcastTipmsg(string.format(Lang.ScriptTips.zf360,Actor.getName(sysarg),itemCfg.addExp, enLinkExpRefineWnd),2048) 
		SendExpRefiningTimes(sysarg)

		-- 记录日志
		local nActorId = Actor.getIntProperty(sysarg, PROP_ENTITY_ID)
		System.sendCommonLog(GameLog.cExpRefining,nActorId,'',0,0,0,'','','','','','','',"GiveOpenServerAward")
	end
end

--经验炼制次数下发
function SendExpRefiningTimes(sysarg)
	local nOpenDay = System.getDaysSinceOpenServer()
	if nOpenDay > #ExpRefiningConfig.ExpRefining then
		return
	elseif nOpenDay == 0 then
		nOpenDay = 1
	end

	local var = Actor.getStaticVar(sysarg)
	if not var.days then var.days = {} end
	local ExpRefiningCount = var.days.ExpRefiningCount
	if ExpRefiningCount == nil then ExpRefiningCount = 0 end
	--[[
	local plv = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)		-- 当前等级
	plv = plv + 1
	if ExpRefiningCount < ExpRefiningConfig.maxCountDay then
		local currExp = Actor.getUIntProperty(sysarg, PROP_ACTOR_EXP)		-- 玩家当前经验
		local awardExp = ExpRefiningConfig.ExpRefining[nOpenDay][ExpRefiningCount + 1].addExp;
		local upExp = LevelUpExp[plv];
		if not upExp then return end
		
		if awardExp >= (upExp - currExp) then
			awardExp = awardExp - (upExp - currExp);
			plv = plv + 1;
			upExp = LevelUpExp[plv];
			if not upExp then return; end
			
			while(awardExp >= upExp) do
				awardExp = awardExp - upExp;
				plv = plv + 1;
				upExp = LevelUpExp[plv];
				if not upExp then return; end
			end
		end
	end
	plv = plv - 1
	]]
	local netPack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sSendExpRefiningTimes)
	DataPack.writeByte(netPack, ExpRefiningCount) 
	--DataPack.writeByte(netPack, plv)
	DataPack.writeByte(netPack, 0)
	DataPack.flush(netPack)
end



MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cGetExpRefining, MiscsImplement.GetExpRefining)